﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="../Content/css/bootstrap.min.css" />
    <link rel="stylesheet" href="../Content/css/font-awesome.min.css" />
    <link rel="stylesheet" href="../Content/css/ace.min.css" />

</head>
<body ng-app="listApp" class="page-content">
    <div ng-controller="listCtrl">
        <div class="row">
            <div class="col-xs-4 col-sm-4">
                <div class="toolbar">
                    <button class="btn btn-sm btn-primary" ng-click="interactive.search()">
                        <i class="icon-search"></i>查询
                    </button>
                    <button class="btn btn-sm btn-success" ng-click="interactive.create()">
                        <i class="icon-pencil"></i>创建
                    </button>
                </div>
                <p></p>
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <td>Name</td>
                                <td>Operation</td>
                            </tr>
                        </thead>
                        <tr ng-repeat="model in modelList">
                            <td><a ng-click="interactive.onSelectModel(model)" class="btn btn-link" href="javascript:void(0)" title="{{model.description}}">{{model.name}}({{model.code}})</a></td>
                            <td style="width: 130px">
                                <div class="btn-group">
                                    <button class="btn btn-xs btn-primary  tooltip-info" data-rel="tooltip" data-placement="bottom"
                                            title="编辑" ng-click="onEdit(model.id)">
                                        编辑
                                    </button>
                                    <button class="btn btn-xs btn-danger tooltip-info" data-rel="tooltip" data-placement="bottom"
                                            title="删除" ng-click="onDelete(article.id)">
                                        删除
                                    </button>
                                    <button class="btn btn-xs btn-primary tooltip-info" data-rel="tooltip" data-placement="bottom"
                                            title="发布" ng-click="interactive.onPublishModel(model)">
                                        发布
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <pagination></pagination>
                </div>
            </div>
            <div class="col-xs-8 col-sm-8">
                <h3 class="header smaller lighter blue">字段列表:{{selectedModel.name}}</h3>
                <div class="toolbar">
                    <button class="btn btn-sm btn-primary" title="添加" ng-click="interactive.addModelField()">
                        <i class="icon-plus">
                        </i>
                        添加
                    </button>
                </div>
                <p></p>
                <div class="table-responsive">
                    <table class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <td>Name</td>
                                <td>Description</td>
                                <td>Operation</td>
                            </tr>
                        </thead>
                        <tr ng-repeat="field in fieldList">
                            <td>{{field.name}}</td>
                            <td>{{field.description}}</td>
                            <td style="width: 130px">
                                <div class="visible-md visible-lg hidden-sm hidden-xs btn-group">
                                    <button class="btn btn-xs btn-primary  tooltip-info" data-rel="tooltip" data-placement="bottom"
                                            title="编辑" ng-click="onEdit(model.id)">
                                        编辑
                                    </button>
                                    <button class="btn btn-xs btn-danger tooltip-info" data-rel="tooltip" data-placement="bottom"
                                            title="删除" ng-click="interactive.deleteModelField(field)">
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
                <h3 class="header smaller lighter blue">关联模型:{{selectedModel.name}}</h3>
                <div class="table-responsive">
                    <div class="toolbar">
                        <button class="btn btn-sm btn-info" ng-click="interactive.onBtnCreateRelationClick()">
                            <i class="icon-link"></i>关联
                        </button>
                    </div>
                    <p></p>
                    <table class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <td>Name</td>
                                <td>
                                    Multiplicity
                                </td>
                                <td>Type</td>
                                <td>Operation</td>
                            </tr>
                        </thead>
                        <tr ng-repeat="relation in relationList">
                            <td>{{relation.model2Name}}</td>
                            <td>{{convertor.relationMultiplicityText(relation)}}</td>
                            <td>{{convertor.relationTypeText(relation)}}</td>
                            <td style="width: 130px">
                                <div class="visible-md visible-lg hidden-sm hidden-xs btn-group">
                                    <button class="btn btn-xs btn-danger tooltip-info" data-rel="tooltip" data-placement="bottom"
                                            title="删除" ng-click="interactive.onDeleteRelation(relation)">
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script src="../Config.js"></script>
    <script src="../Content/js/jquery-1.10.2/jquery-1.10.2.min.js"></script>
    <script src="../Content/js/bootstrap.min.js"></script>
    <script src="../Content/js/promise/Promise.js"></script>
    <script src="../Content/js/ace-elements.min.js"></script>
    <script src="../Content/js/ace.min.js"></script>
    <script src="../Content/js/bootbox.min.js"></script>
    <script src="../Content/js/angular-1.2.29/angular-1.2.29.min.js"></script>

    <script src="../Content/js/jquery.gritter.min.js"></script>
    <script src="../Content/js/cppei/cppei.view.js"></script>
    <script src="../Content/js/cppei/cppei.paginationPlugin.js"></script>
    <script>
        cppei.install();

        var app = angular.module('listApp', []);

        ////加载分页栏
        cppei.paginationPlugin.attach2ngApp(app);

        app.controller("listCtrl", function ($scope, $http) {
            //获取详细信息
            var paginationInst = cppei.paginationPlugin.attach2ngScope($scope, $http, jframe.getUrl('api/Model/UnpublishedList'), {}).success(function (data) {
                cppei.busy(false);
                $scope.modelList = data.items;
            }).error(function (result) {
                cppei.busy(false);
                cppei.growlError(result.message);
            });

            $scope.convertor = {
                relationMultiplicityText: function (relation) {
                    if (relation.model1Multiplicity == 1) {
                        if (relation.model2Multiplicity == 1) {
                            return 'one-one';
                        } else if (relation.model2Multiplicity == 2) {
                            return 'one-one?';
                        } else if (relation.model2Multiplicity == 3) {
                            return 'one-many';
                        } else if (relation.model2Multiplicity == 4) {
                            return 'one-many?';
                        }
                    }
                    return 'unknown';
                },
                relationTypeText: function (relation) {
                    if (relation.type == 1) {
                        return 'aggregation';
                    } else {
                        return 'composition';
                    }
                }
            };

            $scope.service = {
                refresh: function () {
                    cppei.busy(true);
                    paginationInst.reload({});
                },
                loadFieldList: function () {
                    if ($scope.selectedModel) {
                        $http({
                            method: 'get',
                            url: jframe.getUrlWithTimespan('api/Model/FieldList'),
                            params: { modelId: $scope.selectedModel.id }
                        }).success(function (result) {
                            if (result.isSuccess) {
                                debugger;
                                cppei.busy(false);
                                $scope.fieldList = result.data.items;
                            } else {
                                cppei.busy(false);
                                cppei.growlError(result.message);
                            }
                        }).error(function (result) {
                            cppei.busy(false);
                            cppei.growlError('error');
                        });
                    }
                },
                loadRelationList: function () {
                    if ($scope.selectedModel) {
                        $http({
                            method: 'get',
                            url: jframe.getUrlWithTimespan('api/Model/RelationList'),
                            params: { modelId: $scope.selectedModel.id }
                        }).success(function (result) {
                            if (result.isSuccess) {
                                debugger;
                                cppei.busy(false);
                                $scope.relationList = result.data.items;
                            } else {
                                cppei.busy(false);
                                cppei.growlError(result.message);
                            }
                        }).error(function (result) {
                            cppei.busy(false);
                            cppei.growlError('error');
                        });
                    }
                }
            };

            $scope.interactive = {
                create: function () {
                    cppei.switchView(jframe.getUrl('html/model-create.html'));
                },
                search: function () {
                    $scope.service.refresh();
                },
                onPublishModel: function (model) {
                    cppei.confirm('提示', '是否要发布模型[' + model.name + ']?', function (result) {
                        if (result) {
                            $http({
                                method: 'post',
                                url: jframe.getUrl('Api/Model/Publish'),
                                cache: false,
                                data: { id: model.id }
                            }).success(function (result, status) {
                                if (result.isSuccess) {
                                    cppei.growl('发布成功!');
                                    $scope.service.refresh();
                                } else {
                                    cppei.growlError(result.message);
                                }
                            }).error(function (result, status) {
                                cppei.growlError(result.message);
                            });
                        }
                    });
                },
                onSelectModel: function (model) {
                    if ($scope.selectedModel != model) {
                        $scope.selectedModel = model;
                        $scope.service.loadFieldList();
                        $scope.service.loadRelationList();
                    }
                },
                addModelField: function () {
                    cppei.showDialog('创建字段:' + $scope.selectedModel.name, jframe.getUrl('html/modelfield-create.html?id=' + $scope.selectedModel.id), function (result, data) {
                        if (result) {
                            $scope.service.loadFieldList();
                        }
                    }, { width: 400, height: 450 });
                },
                deleteModelField: function (field) {
                    cppei.confirm('提示', '是否要删除字段[' + field.name + ']?', function (result) {
                        if (result) {
                            $http({
                                method: 'post',
                                url: jframe.getUrl('Api/Model/DeleteField'),
                                cache: false,
                                data: { id: field.id }
                            }).success(function (result, status) {
                                if (result.isSuccess) {
                                    cppei.growl('删除成功!');
                                    $scope.service.loadFieldList();
                                } else {
                                    cppei.growlError(result.message);
                                }
                            }).error(function (result, status) {
                                cppei.growlError(result.message);
                            });
                        }
                    });
                },
                onDeleteRelation: function (relation) {
                    cppei.confirm('提示', '是否要删除关联模型[' + relation.model2Name + ']?', function (result) {
                        if (result) {
                            $http({
                                method: 'post',
                                url: jframe.getUrl('Api/Model/DeleteRelation'),
                                cache: false,
                                data: { model1Id: relation.model1Id, model2Id: relation.model2Id }
                            }).success(function (result, status) {
                                if (result.isSuccess) {
                                    cppei.growl('删除成功!');
                                    $scope.service.loadRelationList();
                                } else {
                                    cppei.growlError(result.message);
                                }
                            }).error(function (result, status) {
                                cppei.growlError(result.message);
                            });
                        }
                    });
                },
                onBtnCreateRelationClick: function () {
                    cppei.showDialog('关联模型:' + $scope.selectedModel.name, jframe.getUrl('html/modelrelation-create.html?id=' + $scope.selectedModel.id), function (result, data) {
                        if (result) {
                            $scope.service.loadRelationList();
                        }
                    }, { width: 400, height: 290 });
                }
            };
            $scope.service.refresh();
        });
    </script>
</body>
</html>
